﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NLog;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos;
using WYT.Usr.Application.Contracts.Dtos.GZH;
using WYT.Usr.Application.Contracts.Dtos.User;
using WYT.Usr.Application.Contracts.Dtos.Wallet;
using WYT.Usr.Application.Contracts.Services;

namespace WYT.Usr.WebApi.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("usr/[Controller]")]
    [ApiController]
    public class UserController : AdncControllerBase
    {
        private readonly IUserAppService _userAppService;
        private readonly IUserContext _userContext;
        private readonly IAccountAppService _accountAppService;

        public UserController(IUserAppService userAppService, 
            IUserContext userContext, 
            IAccountAppService accountAppService)
        {
            _userAppService = userAppService;
            _userContext = userContext;
            _accountAppService = accountAppService;
        }

        /// <summary>
        /// 获取个人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("BaseInfo")]
        public async Task<WYTActionResult<UserBaseInfoDto>> GetUserBaseInfo()
        {
            var result = await _userAppService.GetUserBaseInfo(_userContext.Id );

            return Success(result);
        }

        /// <summary>
        /// 获取用户授权信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("AuthorizeCode")]
        public async Task<WYTActionResult<UserAuthorizeCodeDto>> GetUserAuthorizeCode()
        {
            return Success(await _userAppService.GetUserAuthorizeCode(_userContext.Id));
        }

        /// <summary>
        /// 微信授权
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateUserInfor")]
        public async Task<WYTActionResult> WeChatUserInforAuthorization(UserEncryptedInfoDto input)
        {
            return Success(await _userAppService.UpdateUserInfor(_userContext.Id, input));
        }

        /// <summary>
        /// 跟新用户电话号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdatePhone")]
        public async Task<WYTActionResult> UpdateUserPhoneNumber(UserEncryptedInfoDto input)
        {
            return Success(await _userAppService.UpdateUserPhoneNumber(input, _userContext.Id));
        }

        /// <summary>
        /// 更新交易密码
        /// </summary>
        /// <param name="newWord"></param>
        /// <returns></returns>
        [HttpPost("UpdateTPW")]
        public async Task<WYTActionResult> ChangeWithdrawalPasswordAsync(SimpleInputDto<string> newWord)
        {
            var password = newWord.Value;
            //数据校验
            if (string.IsNullOrEmpty(password) || password.Length != 6 || !Regex.IsMatch(password, @"^\d+$"))
                return new WYTActionResult(400,"密码格式错误");

            return Success(await _userAppService.ChangeWithdrawalPassword(_userContext.Id, password));
        }

        /// <summary>
        /// 获取小程序码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("wxaCode")]
        public async Task<WYTActionResult<string>> GetWxaCode(WxaCodeDto input)
        {
            return Success(await _accountAppService.GetWxaCode(input));
        }


        /// <summary>
        /// 给用户发送修改支付密码的短信验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("sendCode")]
        public async Task<WYTActionResult<bool>> SendUserCode()
        {
            return Success(await _userAppService.SendUserCode(_userContext.Id));
        }

        /// <summary>
        /// 验证用户修改支付密码的验证码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost("codeVerify")]
        public async Task<WYTActionResult<bool>> CodeVerify(SimpleDto<string> code)
        {
            return Success(await _userAppService.CodeVerify(_userContext.Id, code.Value));
        }

        /// <summary>
        /// 获取用户交易密码
        /// </summary>
        /// <returns></returns>
        [HttpGet("tpw")]
        public async Task<WYTActionResult<string>> GetWithdrawalPassword()
        {
            return Success(await _userAppService.GetWithdrawalPassword(_userContext.Id));
        }

        /// <summary>
        /// 升级申请
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("upgradeApply")]
        public async Task<WYTActionResult<bool>> UserUpgradeApply(UserUpgradeApplyDto input)
        {
            return Success(await _userAppService.UserUpgradeApply(_userContext.Id, input));
        }

        /// <summary>
        /// 获取团队信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("team")]
        public async Task<WYTActionResult<TeamInfoDto>> GetUserTeamInfor()
        {
            return Success(await _userAppService.GetUserTeamInfor(_userContext.Id));
        }

        /// <summary>
        /// 分页查询用户团队成员
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("TeamMember")]
        public async Task<WYTActionResult<PageModelDto<TeamMemberInfoDto>>> TeamMemberSearch([FromQuery]TeamMemberSearchPagedDto input)
        { 
            return Success(await _userAppService.TeamMemberSearch(_userContext.Id, input));
        }

        /// <summary>
        /// 使用电话号码查找用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("findByPhone")]
        public async Task<WYTActionResult<UserNameAndHeadTelDto>> FindUserByPhone([FromQuery]SimpleDto<string> input)
        {
            if (string.IsNullOrWhiteSpace(input.Value) || !Regex.IsMatch(input.Value, @"^[1]+\d{10}"))
                return new WYTActionResult<UserNameAndHeadTelDto>(400, "参数错误");

            return Success(await _userAppService.FindUserByPhone(input.Value));
        }

        /// <summary>
        /// 修改电话号码（发送验证码）
        /// </summary>
        /// <param name="input">电话号码</param>
        /// <returns></returns>
        [HttpPost("sendChangePhoneCode")]
        public async Task<WYTActionResult> SendChangePhoneCode(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^[1]+\d{10}"))
                return new WYTActionResult(400, "电话号码错误");

            return Success(await _userAppService.SendChangePhoneCode(_userContext.Id,input.Value));
        }

        /// <summary>
        /// 修改电话号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("changePhone")]
        public async Task<WYTActionResult> ChangePhone(TelChangeDto input)
        {
            return Success(await _userAppService.ChangePhone(_userContext.Id, input));
        }

        /// <summary>
        /// 设置用户的上级
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("addSuperior")]
        public async Task<WYTActionResult> AddSuperior (SimpleInputDto<long> input)
        { 
            return Success(await _userAppService.AddSuperior(_userContext.Id, input.Value));
        }

        #region 内部接口

        /// <summary>
        /// 获取用户等级
        /// </summary>
        /// <returns></returns>
        [HttpGet("_grade")]
        public async Task<ActionResult<int>> GetUserGrade()
        {
            return Result(await _userAppService.GetUserGrade(_userContext.Id));
        }

        /// <summary>
        /// 获取用户openId
        /// </summary>
        /// <returns></returns>
        [HttpGet("_openId")]
        public async Task<ActionResult<string>> GetUserOpenId()
        {
            return Result(await _userAppService.GetUserOpenId(_userContext.Id));
        }

        /// <summary>
        /// 获取用户头像和名称
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("_userNameAndHead")]
        public async Task<ActionResult<List<UserNameAndHeadDto>>> GetUserNameAndHead([FromQuery]long[] input)
        {
            return Result(await _userAppService.GetUserNameAndHead(input));
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("_userInfor")]
        public async Task<ActionResult<UserInforDto>> GetUserInfor()
        {
            return Result(await _userAppService.GetUserInfor(_userContext.Id));
        }
        #endregion
    }
}
